package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：徐志荣
 * @Date：2023/6/29 上午 11:57
 */
@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request){
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null ){
            return AjaxResult.fail(-1,"非法请求");
        }
        //没有问题的
        List<ArticleInfo> list = articleService.getMyList(userinfo.getId());
        for (ArticleInfo a :
                list) {
            if(a.getContent().length()>=100){
                a.setContent(a.getContent().substring(0,100)+"...");
            }
        }
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(HttpServletRequest request,Integer id){
        if(id == null || id <= 0){
            //参数有误
            return AjaxResult.fail(-1,"参数异常");
        }
        Userinfo user = UserSessionUtils.getUser(request);
        if(user == null){
            return AjaxResult.fail(-1,"用户未登录");
        }
        return AjaxResult.success(articleService.del(id, user.getId()));
    }

    @RequestMapping("detail")
    public AjaxResult getDetail(Integer id){
        if(id == 0 || id <= 0){
            return AjaxResult.fail(-1,"非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/uprcount")
    public AjaxResult incrRCount(Integer id){
        if(id != null && id > 0){
            return AjaxResult.success(articleService.incrRCount(id));
        }
        return AjaxResult.fail(-1,"非法参数");
    }

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo,HttpServletRequest request){
        //非空校验
        if(articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle()) ||!StringUtils.hasLength(articleInfo.getContent())){
            return AjaxResult.fail(-1,"非法参数");
        }

        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null || userinfo.getId()<=0){
            return AjaxResult.fail(-1,"无效的登录用户");
        }

        articleInfo.setUid(userinfo.getId());

        //自己写错的点  在这里 得去调用 数据库 添加数据
        return AjaxResult.success(articleService.add(articleInfo));
//        return AjaxResult.success(articleInfo);

    }
    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request,ArticleInfo articleInfo){
        // 非空校验
        if(articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())|| !StringUtils.hasLength(articleInfo.getContent()) || articleInfo.getId() == null){
            //非法参数
            return AjaxResult.fail(-1,"非法参数");
        }
        //得到当前登录用户的id
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null ||userinfo.getId() == null){
            return AjaxResult.fail(-2,"无效用户");
        }
        //核心代码 解决了修改文章归属人判定的问题
        articleInfo.setUid(userinfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleInfo));
    }
/*
* 根据分页去查询 列表
* @param pindex  当前页码
* @param psize  条数
* @return
*/
    @RequestMapping("/listbypage")
    public AjaxResult geiListByPage(Integer pindex,Integer psize){
       //1
        if(pindex == null || pindex <= 1){
            pindex = 1;
        }
        if(psize == null || psize <= 1){
            psize = 3;
        }
        int offset = psize * (pindex - 1);
        //文章列表数据
        List<ArticleInfo> list = articleService.geiListByPage(psize,offset);
        for (ArticleInfo a :
                list) {
            if(a.getContent().length()>=100){
                a.setContent(a.getContent().substring(0,100)+"...");
            }
        }
        //当前列表总共有多少页
        // sum % psize != 0  ?  sum / psize +1 : sum / psize
        int totalCount = articleService.getCount();
        double pcountdb = totalCount*1.0/psize;
        //int sum = (int)(pcount + 0.5);
        int pcount =  (int)Math.ceil(pcountdb);

        HashMap<String,Object> result = new HashMap<>();
        result.put("list",list);
        result.put("pcount",pcount);
        return AjaxResult.success(result);

    }


}
